<?php

class Index extends Controller {

        function Index()
        {
                parent::Controller();
        }

        function main()
        {
            $data = array();

                if(isset ($_POST['name'])){

                    //Validate & Prep
                $this->load->library('form_validation');
                $this->form_validation->set_rules('name', 'User Name', 'required|trim|strtolower');
                $this->form_validation->set_rules('pass', 'Password', 'required|trim');

                $result = $this->form_validation->run();

                $data = array();

                if ($result === TRUE)
                {
                        //Check the username and password against the database
                        $this->load->model('user_model');
                        $result = $this->user_model->check_cred($_POST['name'], $_POST['pass']);

                        //If they match, then add a variable to the session cookie
                        if ($result !== FALSE)
                        {
                                $this->session->set_userdata(array('user' => $result));
                                $data['error_msg'] = "Logged In";
                        }
                        else
                                $data['error_msg'] = "The username or password was incorrect!";
                }
                else
                        $data['error_msg'] = "There was a problem with your submission!";

                }

                if(isset ($_POST['vote']))
                    {
                    $this->load->model('add_model');
                    $this->add_model->add_vote($_POST['vote']);
                }

                $this->load->model('get_model');
                $image = $this->get_model->get_rimage();
                $data['image'] = $image;

                    $this->load->view('main_view', $data);
        }

        function user_page()
        {
            $data = array();
            $this->load->helper('form');
            $this->load->model('user_model');

            if($this->session->userdata('user')){
                $this->load->helper('log');

                if(check_user_role() == 1)
                    {
                    $this->load->model('get_model');
                    $data['result'] = $this->get_model->get_useritemlist($this->session->userdata('user'));
                    $this->load->view('admin_view', $data);
                    }
                else
                    {
                    $this->load->model('get_model');
                    $data['result'] = $this->get_model->get_useritemlist($this->session->userdata('user'));
                    $this->load->view('user_view', $data);
                    }
            }
            else
                {
                $data['error_msg'] = "You are not logged in";
                $this->load->view('main_view',$data);
                }
            

        }

        function admin_use()
        {
            $this->load->helper('form');
            $this->load->model('get_model');
            $data['result'] = $this->get_model->get_userlist();

            if(isset ($_POST['remove']))
            {
                $this->load->model('remove_model');
                $this->remove_model->remove_user($_POST['remove']);
            }
            $this->load->view('adminuser_view', $data);
        }

        function admin_img()
        {
            $this->load->helper('form');
            $this->load->model('get_model');
            $data['result'] = $this->get_model->get_itemlist();
            
            if(isset ($_POST['remove']))
                {
                $this->load->model('remove_model');
                $this->remove_model->remove_item($_POST['remove']);
            }
            $this->load->view('adminitem_view', $data);
        }

        function submit()
        {
            ////upload a file from the upload form
            $this->load->helper(array('form', 'url'));
            $file = $this->input->post('userfile');

            //FILE UPLOAD STUFF
            $config['upload_path'] = './image/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size']     = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());

            }
            else
            {
                $data = array('upload_data' => $this->upload->data());

                $upload_data = $this->upload->data();

                $this->load->model('add_model');
                $this->add_model->add_item($upload_data['file_name'], $_POST['note'], $this->session->userdata('user'));
            }
            $this->user_page();

        }

        function uri_form()
        {
            $this->load->model('remove_model');
            if(isset ($_POST))
                {
                $this->remove_model->remove_item($_POST['remove']);
            }
            $this->user_page();
        }

        function register()
        {
            $data = array();

            if(isset ($_POST))
                    {

                $this->load->library('form_validation');
                $this->form_validation->set_rules('name', 'User Name', 'required|trim|strtolower');
                $this->form_validation->set_rules('email', 'Email Address', 'required|trim|strtolower|valid_email');
                $this->form_validation->set_rules('pass', 'Password', 'required|trim');
                $this->form_validation->set_rules('pass2', 'Confirmation', 'required|matches[pass]');


                $result = $this->form_validation->run();

                if ($result === TRUE)
                    {
                    $this->load->model('add_model');
                    $this->add_model->add_user($_POST['name'], $_POST['email'], $_POST['pass']);
                }
                else
                    $data['error_msg'] = "You are not logged in";
            }

            $this->load->view('register_view',$data);
        }

        function result_page()
        {
            $data = array();

            $this->load->model('get_model');
            $result = $this->get_model->get_imageresult();
            $data['result'] = $result;

            $this->load->view('result_view', $data);
        }
}
//EOF

